<?php

namespace app\admin\model;
use think\Model;

class Order extends Model{
    public function getAddTimeAttr($v){
        return date('Y-m-d H:i:s',$v);
    }
    public function searchMaxtimeAttr($query,$value){
        if(!empty($value)){
            $query->whereTime('add_time', '>=', $value);
        }
    }
    public function searchMintimeAttr($query,$value){
        if(!empty($value)){
            $query->whereTime('add_time', '<=', $value);
        }
    }
    public function searchCidAttr($query,$value){
        if(isset($value) && '' !== $value){
            $query->where("status",$value);
        }
    }
    public function searchMaxQianAttr($query,$value){
        if(!empty($value)){
            $query->where('order_amount','>=', $value);
        }
    }
    public function searchMinQianAttr($query,$value){
        if(!empty($value)){
            $query->where('order_amount','<=', $value);
        }
    }
    public function searchKeywordsAttr($query,$value,$data){
        if(''!==$value){
            $query->whereLike('buyer_name','%'.$value.'%');
        }

    }
    public function searchPxAttr($query,$value,$data){
        if(''!==$value){
            $query->order("add_time",$value);
        }
    }
    public static function ord($data=[],$query=[],$page=true){
        $model = self::field('ecm_order.*,ecm_order_status.*')
            ->withSearch(array_keys($data),$data)

            ->join("ecm_order_status","ecm_order.status=ecm_order_status.status_id",'LEFT');

//        if(isset($data['maxtime']) && '' !== $data['maxtime']){
//            $model->whereTime('add_time','>=',$data['maxtime']);
//        }
//        if(isset($data['mintime']) && '' !== $data['mintime']){
//            $model->whereTime('add_time','<=',$data['mintime']);
//        }
//        if(isset($data['cid']) && '' !== $data['cid']){
//            $model->where('status',$data['cid']);
//        }
//        if(isset($data['maxqian']) && '' !== $data['maxqian']){
//            $model->where('order_amount','>=',$data['maxqian']);
//        }
//        if(isset($data['mintime']) && '' !== $data['mintime']){
//            $model->where('order_amount','<=',$data['mintime']);
//        }
//        if(isset($data['keywords']) && '' !== $data['keywords']){
//            $model->whereLike('buyer_name',"%{$data['keywords']}%");
//        }
        if($page){
           return $model->paginate([
            'list_rows'=>10,
            'query'=>$query
        ]);
        }
        return $model->select();
    }
}